#include "spectrogram.h"
#include "qspectrogramview.h"
#include "qaudioreader.h"
#include <QMenuBar>
#include <QMenu>
#include <QHBoxLayout>
#include <QResizeEvent>
#include <QFileDialog>

QSpectrogram::QSpectrogram(QWidget *parent)
    : QWidget(parent)
{
    auto size = QSize( 1200, 800);
    resize(size);

	auto waveFormView = new AudioUtils::QSpectrogramView(this);
    waveFormView->resize(size);
	connect(this, &QSpectrogram::accept, waveFormView, &AudioUtils::QSpectrogramView::open);

    auto menuBar = new QMenuBar(this);
    auto menu = new QMenu(tr("File"));
    auto openAction = new QAction(tr("Open"));
    auto exitAction = new QAction(tr("Close"));

    menu->addAction(openAction);
    menu->addAction(exitAction);
    menuBar->addMenu(menu);
	setContextMenuPolicy(Qt::ActionsContextMenu);

    connect(openAction, &QAction::triggered, this, &QSpectrogram::open);
    connect(exitAction, &QAction::triggered, this, &QSpectrogram::close);

	QHBoxLayout *layout = new QHBoxLayout;
	layout->addWidget(waveFormView);
	setLayout(layout);
}

QSpectrogram::~QSpectrogram()
{
}

void QSpectrogram::open()
{
	auto fileName = QFileDialog::getOpenFileName(this,
		tr("Open Audio"), "", tr("Audio Files (*.wav)"));
	emit accept(fileName);
}
